import Vue from 'vue'
import {
	imageUrl
} from 'common/config.js'
import request from './common/api/request.js'

Vue.prototype.$imageHost = imageUrl

// 跳转方便统一改样式
Vue.prototype.$linkTo = function(url, option) {
	console.log(url, option)
	option = option || {};
	option.url = url;
	uni.navigateTo(option);
}

// 重定向跳转方便统一改样式
Vue.prototype.$redirectTo = function(url, option) {
	option = option || {};
	option.url = url;
	uni.redirectTo(option);
}

Vue.prototype.$navigateBack = function() {
	uni.navigateBack()
}

Vue.prototype.$message = {
	info(title, options) {
		options = options || {};
		uni.showToast({
			title,
			duration: options.duration || 2000
		});
	},
	error() {
		return this.info(...arguments)
	}
}

Vue.prototype.$request = request

Vue.prototype.$dateFormat = function(timestamp, formats) {
	// formats格式包括
	// 1. Y-m-d
	// 2. Y-m-d H:i:s
	// 3. Y年m月d日
	// 4. Y年m月d日 H时i分
	// 5. Y.m.d
	// 6. 自定义格式
	formats = formats || 'Y-m-d';

	var zero = function(value) {
		if (value < 10) {
			return '0' + value;
		}
		return value;
	};

	var myDate = timestamp ? new Date(timestamp) : new Date();

	var year = myDate.getFullYear();
	var month = zero(myDate.getMonth() + 1);
	var day = zero(myDate.getDate());

	var hour = zero(myDate.getHours());
	var minite = zero(myDate.getMinutes());
	var second = zero(myDate.getSeconds());

	return formats.replace(/Y|m|d|H|i|s/ig, function(matches) {
		return ({
			Y: year,
			m: month,
			d: day,
			H: hour,
			i: minite,
			s: second
		})[matches];
	});
}
Vue.prototype.$dateFormat1 = function(timestamp, formats) {
	// formats格式包括
	// 1. Y-m-d
	// 2. Y-m-d H:i:s
	// 3. Y年m月d日
	// 4. Y年m月d日 H时i分
	// 5. Y.m.d
	// 6. 自定义格式
	formats = formats || 'Y-m-d';

	var zero = function(value) {
		// if (value < 10) {
		// 	return '0' + value;
		// }
		return value;
	};

	var myDate = timestamp ? new Date(timestamp) : new Date();

	var year = myDate.getFullYear();
	var month = zero(myDate.getMonth() + 1);
	var day = zero(myDate.getDate());

	var hour = zero(myDate.getHours());
	var minite = zero(myDate.getMinutes());
	var second = zero(myDate.getSeconds());

	return formats.replace(/Y|m|d|H|i|s/ig, function(matches) {
		return ({
			Y: year,
			m: month,
			d: day,
			H: hour,
			i: minite,
			s: second
		})[matches];
	});
}
//金额格式转换
Vue.prototype.$amountCvs = function(num, n) {
	num=num===''?0:num
	// num 金额
	// n 保留小数位数
	n = n || 2 //设置保留的小数位数
	num = parseFloat((num + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
	if(num=='NaN'){
		return 0
	}
	var l = num.split(".")[0].split("").reverse();
	var r = num.split(".")[1];
	var t = "";
	for (var i = 0; i < l.length; i++) {
		t += l[i];
	}
	return t.split("").reverse().join("") + "." + r;
}

//dateTimeStamp是一个时间毫秒，注意时间戳是秒的形式，在这个毫秒的基础上除以1000，就是十位数的时间戳。13位数的都是时间毫秒。

Vue.prototype.$timeago = function(dateTimeStamp){  
     var minute = 1000 * 60;      //把分，时，天，周，半个月，一个月用毫秒表示
     var hour = minute * 60;
     var day = hour * 24;
     var week = day * 7;
     var halfamonth = day * 15;
     var month = day * 30;
     var now = new Date().getTime();   //获取当前时间毫秒
     var diffValue = now - dateTimeStamp;//时间差
 
 
	var result
     if(diffValue < 0){
         return;
     }
     var minC = diffValue/minute;  //计算时间差的分，时，天，周，月
     var hourC = diffValue/hour;
     var dayC = diffValue/day;
     var weekC = diffValue/week;
     var monthC = diffValue/month;
     if(monthC >= 1 && monthC <= 3){
         result = " " + parseInt(monthC) + "月前"
     }else if(weekC >= 1 && weekC <= 3){
         result = " " + parseInt(weekC) + "周前"
     }else if(dayC >= 1 && dayC <= 6){
         result = " " + parseInt(dayC) + "天前"
     }else if(hourC >= 1 && hourC <= 23){
         result = " " + parseInt(hourC) + "小时前"
     }else if(minC >= 1 && minC <= 59){
         result =" " + parseInt(minC) + "分钟前"
     }else if(diffValue >= 0 && diffValue <= minute){
         result = "刚刚"
     }else {
         var datetime = new Date();
         datetime.setTime(dateTimeStamp);
         var Nyear = datetime.getFullYear();
         var Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
         var Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
         var Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
         var Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
         var Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
         result = Nyear + "-" + Nmonth + "-" + Ndate
     }
     return result;
 }
 
 
 const qiniuUploader=require("./js_sdk/qiniuUploader.js");
 
 Vue.prototype.$UploadImg=function(filePath,uptoken){
	 var promise=new Promise(function(resolve,reject){
		 uni.showLoading({
		 	title:"加载中",
			mask:true
		 })
		 qiniuUploader.upload(filePath,(res)=>{
			 uni.hideLoading()
			 resolve(res)
		 },
		 (error)=>{
			 uni.hideLoading()
			 reject(error)
		 },
		 {
			 region:'ECN',
			 uploadURL:'https://up-z0.qiniup.com',
			 domain:'qiniu.feelmao.com',
			 uptoken:uptoken
		 })
	 })
	 return promise
 }


